package com.gitee.neuray.security.generator.strategy;

import com.gitee.neuray.security.generator.enums.GenerateBizParmsEnum;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.util.*;

/**
 * @author: zhhongyu
 * @description: 多表生成策略
 * @since: Create in 13:15 2020/2/19
 */
public class GenMultiTableStrategy extends GenerateStrategy{
    @Override
    public VelocityContext getVelocityContext(Configuration config, GenBaseBizEntityStrategy bizParams, Map<String, String> table, List<Map> columns) {
        boolean hasBigDecimal = false;
        GenBaseTableEntityStrategy tableEntity = getTableEntity(bizParams, table);
        List<GenBaseColumnEntityStrategy> allcolumn = getColumns(columns, config, tableEntity, hasBigDecimal);
        tableEntity.setColumns(allcolumn);
        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        //封装模板数据\
        Map<String, Object> map = getMap(tableEntity);
        map.put("columns", tableEntity.getColumns());
        map.put("hasBigDecimal", hasBigDecimal);
        map.put("sql",table.get(GenerateBizParmsEnum.sql.name()));
        map.putAll(getVueContextMap(config,bizParams));
        VelocityContext context = new VelocityContext(map);
        return context;
    }

    @Override
    public List<String> getTemplates() {
        List<String> templates = new ArrayList<String>();
        templates.add("template/multitable/Vo.java.vm");
        templates.add("template/multitable/Mapper.java.vm");
        templates.add("template/multitable/Mapper.xml.vm");
        templates.add("template/multitable/Biz.java.vm");
        templates.add("template/multitable/Controller.java.vm");
        templates.add("template/multitable/PageExcel.java.vm");
        templates.addAll(getVueTemplate());
        return templates;
    }

    @Override
    public String getFileName(String template, String className, String packageName, String moduleName,String vuePath,String serverName) {
        String packagePath = "main" + File.separator + "java" + File.separator;
        if (StringUtils.isNotBlank(packageName)) {
            packagePath += packageName.replace(".", File.separator) + File.separator + serverName + File.separator + moduleName + File.separator;
        }

        if (template.contains("Vo.java.vm")) {
            return packagePath + "vo" + File.separator + className + "Vo.java";
        }
        if (template.contains("PageExcel.java.vm")) {
            return packagePath + "vo" + File.separator + className + "PageExcelVo.java";
        }

        if (template.contains("Mapper.java.vm")) {
            return packagePath + "mapper" + File.separator + className + "Mapper.java";
        }
        if (template.contains("Biz.java.vm")) {
            return packagePath + "biz" + File.separator + className + "Biz.java";
        }

        if (template.contains("Controller.java.vm")) {
            return packagePath + "controller" + File.separator + className + "Controller.java";
        }

        if (template.contains("Mapper.xml.vm")) {
            return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + moduleName + File.separator + className + "Mapper.xml";
        }
        //配置vue路径
        if(template.contains("vm2.vue.vm")){
            return vuePath + File.separator + serverName + File.separator + moduleName + File.separator + className.toLowerCase() + ".vue";
        }
        return null;
    }
}
